<?php
/**
 * Created by PhpStorm.
 * User: dung
 * Date: 11/13/14
 * Time: 11:47 AM
 */

class MyModel
{
    public function listAllItems($className, $options = null)
    {
        $key = $className . '-all-' . date('d-m-Y');
        $items = array();
        if (Cache::has($key)) {
            $items = Cache::get($key);
        } else {
            $items = $className::all();
            Cache::put($key, $items, 60);
        }


        return $items;
    }

    public function listAllItemsToArray($className, $arrParams, $options = array())
    {
        $items = $this->listAllItems($className);
        $array = array();
        $array[] = 'Chọn';
        foreach ($items as $item) {
            $array[$item->{$arrParams['value']}] = $item->{$arrParams['display']};
        }
        return $array;
    }

    public function listItems($className, $arrParams, $options = array())
    {
        $class = new $className();

        if (isset($options['cached']) && Cache::has($options['cached']['key'])) {
            $class = Cache::get($options['cached']['key']);
        }

        if (isset($arrParams['paginate']['appends']['withTrashed']) && $arrParams['paginate']['appends']['withTrashed'] == 1)
            $class = $class->withTrashed();
        if (isset($arrParams['paginate']['appends']['onlyTrashed']) && $arrParams['paginate']['appends']['onlyTrashed'] == 1)
            $class = $class->onlyTrashed();

        if (isset($arrParams['joins'])) {
            foreach ($arrParams['joins'] as $join) {
                $class = $class->join($join['table'], $join['pk'], '=', $join['fk']);
            }
        }
        if (isset($arrParams['conditions'])) {
            foreach ($arrParams['conditions'] as $condition) {
                $class = $class = $class->where($condition['field'], $condition['opr'], $condition['value']);
            }
        }

        if (isset($arrParams['left_joins'])) {
            foreach ($arrParams['left_joins'] as $join) {
                $class = $class->leftJoin($join['table'], $join['pk'], '=', $join['fk']);
            }
        }
        if (isset($arrParams['orders'])) {
            $class = $class->orderBy($arrParams['orders']['field'], $arrParams['orders']['type']);
        }
        if (isset($arrParams['take'])) {
            $class = $class->take($arrParams['take']);
        }
        if (isset($arrParams['selects'])) {
            $class = $class->select($arrParams['selects']);
        }
        if (isset($arrParams['first'])) {
            $class = $class->first();
        }
        if (isset($arrParams['paginate'])) {
            $class = $class->paginate(!empty($arrParams['paginate']['nums']) ? $arrParams['paginate']['nums'] : 25);
            if (isset($arrParams['paginate']['appends'])) {
                $class = $class->appends($arrParams['paginate']['appends']);
            }
        } else {
            $class = $class->get();
        }
        return $class;

    }

    public function listTop($className, $arrParams, $nameCache = '', $options = array())
    {
        $key = $className . '-' . $nameCache . '-' . $arrParams['top'] . '-' . date('dmY');
        $arrParams['orders'] = array('field' => $arrParams['top'], 'type' => 'DESC');
        $array = array();
        if (Cache::has($key))
            $array = Cache::get($key);
        else {
            $array = $this->listItems($className, $arrParams);
            Cache::put($key, $array, 60);
        }
        return $array;
    }

    public function listAllItemsToCache($className, $arrParams)
    {
        $key = 'all-item-' . $className . '-' . date('dmY');
        $array = array();
        if (Cache::has($key))
            $array = Cache::get($key);
        else {
            $array = $this->listItems($className, $arrParams);
        }
        return $array;
    }

    public function saveItem($className, $arrParams, $options = array())
    {
        $class = !empty($arrParams['id']) ? $className::find($arrParams['id']) : new $className();
        foreach ($arrParams as $key => $value) {
            if ($key != 'id')
                $class->{$key} = $value;
        }
        $class->save();
        if (isset($options['return']) && $options['return'] == 'class')
            return $class;
        return $class->id;
    }
} 